We've been talking
about a card class, and cards usually
belong to a deck, and a deck is a
collection or a list of 52 cards from ace to
king in the four suits. How does that look in Python? Here we have a card class
followed by a deck class. If I execute using
the Python Tutor, I see that I've defined now the card class and it has the
unit and the string method. It also has rank names and suit names as lists
of these things. These exists on the class. They're called class attributes rather than object attributes, because not every card object needs to have the same list. It would just be a
lot of wasted space. Instead we put it on the class. Now we've created
the deck class. It has the net and the
string function for that, we have our main function, and then we're going to
call the main function. In there we're going to
initialize or create a deck. When I create the deck,
of course calls the init in the deck class. We set the card list
to the empty list, and then we loop through for I in range of all
the suit names, so zero to the length
of the suit names and then zero to the
length of the rank names, and then we create that card. When I create a card, we call the card init method. We set that objects suit. We see that happening here, and that its rank. Now that's the ace of clubs, and we add it to this list. Then of course we can
do the next thing, create the next card, and that'll create the next one, and we see that we've now
created the two of clubs. This code will continue to
repeat through from ace to 2, to 3 to 4 all the way
up to king in clubs. Then it'll do the next
suit and the next suit. That's how card and
deck work together.